package com.ruoyi.oa.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.oa.domain.bo.OaAdminEntryBo;
import com.ruoyi.oa.domain.vo.OaAdminEntryVo;
import com.ruoyi.oa.domain.vo.OaTotalAssetVo;
import com.ruoyi.workflow.utils.WorkFlowUtils;
import lombok.RequiredArgsConstructor;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Service;
import com.ruoyi.oa.domain.bo.OaAdminAssetBo;
import com.ruoyi.oa.domain.vo.OaAdminAssetVo;
import com.ruoyi.oa.domain.OaAdminAsset;
import com.ruoyi.oa.mapper.OaAdminAssetMapper;
import com.ruoyi.oa.service.IOaAdminAssetService;

import java.util.List;
import java.util.Map;
import java.util.Collection;
import java.util.stream.Collectors;

/**
 * 资产管理Service业务层处理
 *
 * @author ruoyi
 * @date 2023-08-28
 */
@RequiredArgsConstructor
@Service
public class OaAdminAssetServiceImpl implements IOaAdminAssetService {

    private final OaAdminAssetMapper baseMapper;

    /**
     * 查询资产管理
     */
    @Override
    public OaAdminAssetVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询资产管理列表
     */
    @Override
    public TableDataInfo<OaAdminAssetVo> queryPageList(OaAdminAssetBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<OaAdminAsset> lqw = buildQueryWrapper(bo);
        Page<OaAdminAssetVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询资产管理列表
     */
    @Override
    public List<OaAdminAssetVo> queryList(OaAdminAssetBo bo) {
        LambdaQueryWrapper<OaAdminAsset> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<OaAdminAsset> buildQueryWrapper(OaAdminAssetBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<OaAdminAsset> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getAssetType()), OaAdminAsset::getAssetType, bo.getAssetType());
        lqw.eq(StringUtils.isNotBlank(bo.getAssetCode()), OaAdminAsset::getAssetCode, bo.getAssetCode());
        lqw.like(StringUtils.isNotBlank(bo.getProductName()), OaAdminAsset::getProductName, bo.getProductName());
        lqw.eq(bo.getInputNum() != null, OaAdminAsset::getInputNum, bo.getInputNum());
        lqw.eq(bo.getLeaveCount() != null, OaAdminAsset::getLeaveCount, bo.getLeaveCount());
        lqw.eq(StringUtils.isNotBlank(bo.getRemarks()), OaAdminAsset::getRemarks, bo.getRemarks());
        return lqw;
    }

    /**
     * 新增资产管理
     */
    @Override
    public Boolean insertByBo(OaAdminAssetBo bo) {
        OaAdminAsset add = BeanUtil.toBean(bo, OaAdminAsset.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改资产管理
     */
    @Override
    public Boolean updateByBo(OaAdminAssetBo bo) {
        OaAdminAsset update = BeanUtil.toBean(bo, OaAdminAsset.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(OaAdminAsset entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除资产管理
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    /**
     * 获取所有资产编码
     * @return
     */
    public List<String> getAllStandard(String assetType){
        return baseMapper.getAllStandard(assetType);
    }


    @Override
    public List<String> getAllProduct(String assetType){
        return baseMapper.getAllProduct(assetType);
    }


    /**
     * 通过编码获取所有物品
     * @param assetCode
     * @return
     */
    @Override
    public List<String> getAllProductNameByAssetCode(String assetType,String assetCode) {
        return baseMapper.getAllProductNameByAssetCode(assetType,assetCode);
    }

    @Override
    public List<String> getAllAssetCodeByProductName(String assetType, String productName) {
        return baseMapper.getAllAssetCodeByProductName(assetType, productName);
    }

    /**
     * 判断是否存在数据
     * @param assetCode
     * @param productName
     * @return
     */
    @Override
    public OaAdminAsset checkHasOneData(String assetType,String assetCode, String productName) {
        return baseMapper.checkHasOneData(assetType,assetCode, productName);
    }

    @Override
    public TableDataInfo<OaTotalAssetVo> getAssetHistory(OaAdminEntryBo bo, PageQuery pageQuery) {
        List<OaTotalAssetVo> voList = baseMapper.getAssetHistory(bo);
        // 添加流程信息
        if (CollectionUtil.isNotEmpty(voList)) {
            List<String> list = voList.stream().map(OaTotalAssetVo::getId).collect(Collectors.toList());
            WorkFlowUtils.setStatusListFileValue(voList, list, "id");
        }
        if (CollectionUtil.isNotEmpty(voList)) {
            voList = voList.stream().filter(d -> d.getActBusinessStatus().getStatus().equals("finish")).collect(Collectors.toList());
        }
        Page<OaTotalAssetVo> result = new Page<>();
        result.setTotal(voList.size());
        int start = (pageQuery.getPageNum() - 1) * pageQuery.getPageSize();
        int end = Math.min(pageQuery.getPageNum() * pageQuery.getPageSize(), voList.size());
        voList = voList.subList(start, end);
        result.setRecords(voList);
        result.setCurrent(pageQuery.getPageNum());
        result.setSize(pageQuery.getPageSize());
        return TableDataInfo.build(result);
    }
}
